package com.cloudweb.oa.config;

import com.cloudwebsoft.framework.util.LogUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 用于内置Tomcat运行时，启动后自动打开IE
 */
@Component
public class MyCommandRunner implements CommandLineRunner {

    @Value("${spring.web.setupUrl}")
    private String setupUrl;

    @Value("${spring.web.backUrl}")
    private String backUrl;

    @Value("${spring.web.browser}")
    private String browser;

    @Value("${spring.web.chromePath}")
    private String chromePath;

    @Value("${spring.web.autoOpen}")
    private boolean isOpen;

    @Override
    public void run(String... args) throws Exception {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            if (isOpen) {
                // 进入setup界面
                // Runtime.getRuntime().exec("cmd /c start " + setupUrl);

                // 进入后端
                Runtime.getRuntime().exec("cmd /c start " + backUrl);

                /*if ("chrome".equalsIgnoreCase(browser)) {
                    String cmd = chromePath + " " + setupUrl;
                    Runtime run = Runtime.getRuntime();
                    try {
                        run.exec(cmd);
                        logger.debug("启动浏览器打开项目成功");
                    } catch (Exception e) {
                        LogUtil.getLog(getClass()).error(e);
                    }
                }
                else {
                    try {
                        // 打开IE
                        // Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe " + loginUrl);
                        // 打开默认浏览器
                        // Runtime.getRuntime().exec("cmd /c start " + loginUrl);
                    } catch (Exception ex) {
                        LogUtil.getLog(getClass()).error(ex);
                    }
                }*/
                }
        }

    }
}